package com.kiragame.xchat.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.kiragame.xchat.ProcessImage;
import com.kiragame.xchat.R;
import com.kiragame.xchat.service.ChatService;
import com.kiragame.xchat.utils.Common;
import com.kiragame.xchat.utils.DataManager;
import com.kiragame.xchat.utils.KiraGameLog;

public class ProfileFragment extends Fragment {

	private ImageView ivAvatar;
	private EditText etName;
	private EditText etStatus;
	private Button btEdit;
	private Button btLogout;
	private AQuery aq = new AQuery(getActivity());

	public static ProfileFragment newInstance() {
		ProfileFragment f = new ProfileFragment();
		// Bundle b = new Bundle();
		// b.putInt(ARG_POSITION, position);
		// f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// position = getArguments().getInt(ARG_POSITION);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_profile, container,
				false);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		etName.setText(DataManager.getInstance(getActivity()).getUserName());
		etStatus.setText(DataManager.getInstance(getActivity()).getUserStatus());
		String url = DataManager.getInstance(getActivity()).getUserAvatar();
		if (url != null) {
			showAvatar(url);
		}
	}

	private void setEvent() {
		ivAvatar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectImage();
			}
		});
		btEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String avatarURL = DataManager.getInstance(getActivity())
						.getUserAvatar();
				String name = etName.getText().toString();
				String status = etStatus.getText().toString();

				ChatService.getInstance()
						.updateProfile(name, status, avatarURL);
			}
		});
		
		btLogout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ChatService.getInstance().logOut();
//				getActivity().finish();
			}
		});
	}

	protected void selectImage() {
		Intent intent = new Intent(getActivity(), ProcessImage.class);
		intent.putExtra(ProcessImage.IS_SELECT_IMAGE_FROM_CAMERA, false);
		intent.putExtra(ProcessImage.ASPECT_RATIO_X, 10);
		intent.putExtra(ProcessImage.ASPECT_RATIO_Y, 10);
		startActivityForResult(intent, Common.RequestCode.EDIT_AVATAR_PROFILE);
	}

	private void findViewIds(View view) {
		ivAvatar = (ImageView) view.findViewById(R.id.iv_avatar);
		etName = (EditText) view.findViewById(R.id.et_name);
		etStatus = (EditText) view.findViewById(R.id.et_status);
//		btEdit = (Button) view.findViewById(R.id.bt_edit);
//		btLogout = (Button) view.findViewById(R.id.bt_logout);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		KiraGameLog.d("call here with  code " + requestCode);
		switch (requestCode) {
		case Common.RequestCode.EDIT_AVATAR_PROFILE:
			String url = data.getStringExtra(ProcessImage.IMAGE_URL);
			if (url != null) {
				DataManager.getInstance(getActivity()).setUserAvatar(url);
				showAvatar(url);
			}
			break;

		default:
			break;
		}
	}

	private void showAvatar(String url) {
		ImageOptions options = new ImageOptions();
		options.round = 15;
		options.fileCache = true;
		options.memCache = true;
		options.targetWidth = 96;
		// options.fallback =
		aq.id(ivAvatar).image(url, options);
	}

}
